from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet

from apps.django_admin.serializer.sku import SKUGoodSeralizer, SKUCategorieSerializer, SPUSpecSerialzier
from apps.django_admin.utils import PageNum
from apps.goods.models import SKU, GoodsCategory, SPUSpecification


class SKUGoodsView(ModelViewSet):
    serializer_class = SKUGoodSeralizer
    pagination_class = PageNum

    def get_queryset(self):

        keyword = self.request.query_params.get('keyword')
        if keyword is None:
            return SKU.objects.all()
        return SKU.objects.filter(name__contains=keyword)


class Categories(ListAPIView):
    serializer_class = SKUCategorieSerializer
    queryset = GoodsCategory.objects.filter(subs__isnull=True)


class goods_specs(ListAPIView):
    serializer_class = SPUSpecSerialzier

    def get_queryset(self):
        pk = self.kwargs.get('pk')

        return SPUSpecification.objects.filter(spu_id=pk)


